import { getBaseRangeUrl, socialSecurityCalculateUrl } from '../../../../api/index'
import { post, get } from '../../../../utils/index'
import WxValidate from '../../../../utils/WxValidate'

Page({
  data: {
    form: { socialSecurityType: 0, socialSecurityTypeName: '本地', accumulationFundScaleType: 2, accumulationFundScaleTypeName: '企业12% + 个人12%' },
    formError: {},
    socialSecurityTypePopup: false,
    accumulationFundScaleTypePopup: false,
    socialSecurityTypeList: [{ id: 0, name: '本地' }, { id: 1, name: '外地' }],
    // accumulationFundScaleTypeList: [{ id: 0, name: '企业8% + 个人8%' }, { id: 2, name: '企业12% + 个人12%' }],
    accumulationFundScaleTypeList: [{ id: 2, name: '企业12% + 个人12%' }],
    lowestWage: 1700,
    averageWageMax: 18864
  },
  async submit () {
    const { form } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      wx.showToast({ title: '信息填写有误，请检查', icon: 'none' })
      return
    }
    const { data } = await post(socialSecurityCalculateUrl, form)
    wx.setStorageSync('result', data)
    wx.navigateTo({ url: '/pages/individual/work/result/result' })
  },
  async getBaseRange () { // 查询社保和公积金基数范围
    const { socialSecurityType } = this.data.form
    const { data: { lowestWage, averageWageMax } } = await get(getBaseRangeUrl, { socialSecurityType })
    this.setData({ lowestWage, averageWageMax })
    this.initValidator()
  },
  socialSecurityTypeConfirm ({ detail }) { // 社保类型确定
    const { value } = detail
    this.setData({
      'form.socialSecurityType': value.id,
      'form.socialSecurityTypeName': value.name
    })
    this.socialSecurityTypeClose()
    this.getBaseRange()
  },
  socialSecurityTypeClick () { // 社保类型弹窗
    wx.hideKeyboard()
    this.setData({ socialSecurityTypePopup: true })
  },
  socialSecurityTypeClose () { // 社保类型取消
    this.setData({ socialSecurityTypePopup: false })
  },
  socialSecurityBaseChange ({ detail }) { // 社保基数
    this.setData({ 'form.socialSecurityBase': detail })
  },
  accumulationFundScaleTypeConfirm ({ detail }) { // 公积金比例类型确定
    const { value } = detail
    this.setData({
      'form.accumulationFundScaleType': value.id,
      'form.accumulationFundScaleTypeName': value.name
    })
    this.accumulationFundScaleTypeClose()
  },
  accumulationFundScaleTypeClick () { // 公积金比例类型弹窗
    wx.hideKeyboard()
    this.setData({ accumulationFundScaleTypePopup: true })
  },
  accumulationFundScaleTypeClose () { // 公积金比例类型取消
    this.setData({ accumulationFundScaleTypePopup: false })
  },
  accumulationFundBaseChange ({ detail }) { // 公积金基数
    this.setData({ 'form.accumulationFundBase': detail })
  },
  initValidator () { // 初始化验证器
    const { lowestWage, averageWageMax } = this.data
    const rules = {
      accumulationFundBase: { required: true, number: true, range: [lowestWage, averageWageMax] },
      accumulationFundScaleType: { required: true },
      socialSecurityBase: { required: true, number: true, range: [lowestWage, averageWageMax] },
      socialSecurityType: { required: true }
    }
    const messages = {
      accumulationFundBase: { required: '请输入公积金基数' },
      accumulationFundScaleType: { required: '请选择公积金比例' },
      socialSecurityBase: { required: '请输入社保基数' },
      socialSecurityType: { required: '请选择社保类型' }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  onLoad () {
    this.getBaseRange()
  },
  onUnload () {
    wx.removeStorageSync('result')
  }
})